<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Blog_post extends DataMapper {

	private $table_name	= 'blog_posts';
	var $auto_populate_has_many = TRUE;
	var $auto_populate_has_one = TRUE;
	
	var $has_one = array(
		'category' => array(
			'class' => 'blog_category',
            'other_field' => 'posts',
            'join_self_as' => 'blog_post',
            'join_other_as' => 'blog_category',
            'join_table' => 'r_blog_categories_blog_posts'),
            
		'author' => array(
			'class' => 'user',
            'other_field' => 'posts',
            'join_self_as' => 'blog_post',
            'join_other_as' => 'user',
            'join_table' => 'r_blog_posts_users')
    );
    

    var $validation = array(
        'title' => array(
            'label' => 'Title',
            'rules' => array('required', 'trim', 'unique', 'alpha_dash', 'min_length' => 4, 'max_length' => 128),
        ),
        'subtitle' => array(
            'label' => 'Subtitle',
            'rules' => array('required', 'trim', 'alpha_dash', 'max_length' => 128),
        ),
        'text' => array(
            'label' => 'Text',
            'rules' => array('required', 'trim'),
        ),
	    'author' => array(
			'label' => 'User',
			'rules' => array('required', 'trim', 'is_int'),
	    ),
	    'published' => array(
	    	'label' => 'Published',
			'rules' => array('required', 'trim', 'valid_match' => array(0, 1))
	    ),
	    'deleted' => array(
			'label' => 'Deleted',
			'rules' => array('required', 'trim', 'valid_match' => array(0, 1))
	    ),
	    'lang' => array(
			'label' => 'Language',
			'rules' => array('required', 'trim', 'min_length' => 5, 'min_length' => 5)
	    )
    );
    
	function __construct()
	{
		parent::__construct();
	}
	
	// posts
	
	static function count_results($category = NULL, $date = NULL) {
		$ci =& get_instance();
		
		$c = new Blog_category();
		if($category == NULL) {
			$c->get();
		}
		else {
			$c->get_by_id($category);
		}
		$c->posts->get_by_lang($ci->session->userdata('uilang'));
		return count($c->posts->all);
    }
}

?>
